package org.dashboard.security;

import org.dashboard.bean.AdUser;
import org.dashboard.service.AdUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.alibaba.dubbo.config.annotation.Reference;
import java.util.Collections;
import java.util.Date;


/**
 * Copyright © 2018. All rights reserved.
 *
 * @Author: FreezeSoul
 * @CreateDate: 2018/4/13 10:32
 * @Description:
 * @Version: V1.0
 */
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    @Reference
    private AdUserService adUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AdUser user = adUserService.selectByName(username);

        if (user == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
        } else {
            return JwtUserFactory.create(user.getUserId().toString(), user.getUserName(), user.getUserPassword(), Collections.emptyList(), true, null );
        }
    }
}
